package org.libra {
	import flash.events.Event;
	import flash.events.MouseEvent;
	import org.libra.basic.BasicButton;
	import org.libra.basic.Container;
	import org.libra.geom.IntDimension;
	import org.libra.geom.IntPoint;

	/**
	 * ...
	 * @author zhangzuoqiang
	 */
	public class JMenuItem extends BasicButton {
		
		private var menuItems:Vector.<JMenuItem>;
		private var childPanel:Container;

		public function JMenuItem(text:String) { 
			super(text);
			menuItems = new Vector.<JMenuItem>();
			childPanel = new Container();
		}
		
		public function appendItem(item:JMenuItem):void {
			if (menuItems.indexOf(item) == -1) {
				item.setLocation(new IntPoint(0, childPanel.getHeight()));
				childPanel.append(item);
				childPanel.setSize(new IntDimension(item.getWidth(), childPanel.getHeight() + item.getHeight()));
				this.menuItems[menuItems.length] = item;
			}
		}
		
		public function appendAllItems(...items):void {
			for (var i:*in items) {
				this.appendItem(items[i]);
			}
		}
		
		public function removeItem(item:JMenuItem):void {
			var index:int = menuItems.indexOf(item);
			if (index != -1) {
				var l:int = menuItems.length - 1;
				for (var i:int = index; i < l; i += 1 ) {
					menuItems[i + 1].setLocation(menuItems[i].getLocation());
				}
				childPanel.remove(item);
				childPanel.setSize(new IntDimension(childPanel.getWidth(), childPanel.getHeight() - menuItems[index].getHeight()));
				this.menuItems.splice(index, 1);
			}
		}
		
		public function clearItem():void {
			this.menuItems.length = 0;
			childPanel.removeAll();
			childPanel.setSize(new IntDimension());
		}
		
		public function getChildPanel():Container {
			return this.menuItems.length > 0 ? this.childPanel : null;
		}
		
		override public function dispose():void {
			super.dispose();
			for each(var item:JMenuItem in menuItems) {
				item.dispose();
			}
		}
		
		override public function toString():String {
			return "JMenuItem";
		}

	}

}